

package com.bill99.util;

import java.text.DateFormat;
import java.text.DateFormatSymbols;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Locale;

public class DateUtil {
	private DateUtil() {
	}

	public static String formatDateTime(String pattern) {
		SimpleDateFormat sdf = new SimpleDateFormat(pattern);
		String now = sdf.format(new Date());
		return now;
	}

	public static String formatDateTime(String pattern, Date date) {
		String strDate = null;
		SimpleDateFormat dateFormat = null;
		if (date == null) {
			return "";
		} else {
			dateFormat = new SimpleDateFormat(pattern);
			strDate = dateFormat.format(date);
			return strDate;
		}
	}

	public static String formatDateTime(String pattern, Date date, Locale locale) {
		String strDate = null;
		SimpleDateFormat dateFormat = null;
		if (date == null) {
			return "";
		} else {
			dateFormat = new SimpleDateFormat(pattern, locale);
			strDate = dateFormat.format(date);
			return strDate;
		}
	}

	public static Date parse(String pattern, String strDateTime) {
		Date date = null;
		if (strDateTime != null && pattern != null) {
			try {
				SimpleDateFormat formatter = new SimpleDateFormat(pattern);
				formatter.setLenient(false);
				date = formatter.parse(strDateTime);
			} catch (ParseException var4) {
				var4.printStackTrace();
			}

			return date;
		} else {
			return null;
		}
	}

	public static Date composeDate(Date date, Date time) {
		if (date != null && time != null) {
			Calendar c1 = Calendar.getInstance();
			c1.setTime(date);
			Calendar c2 = Calendar.getInstance();
			c2.setTime(time);
			Calendar c3 = Calendar.getInstance();
			c3.set(c1.get(1), c1.get(2), c1.get(5), c2.get(11), c2.get(12), c2.get(13));
			return c3.getTime();
		} else {
			return null;
		}
	}

	public static Date getTheDate(Date date) {
		if (date == null) {
			return null;
		} else {
			Calendar c1 = Calendar.getInstance();
			c1.setTime(date);
			Calendar c2 = Calendar.getInstance();
			c2.set(c1.get(1), c1.get(2), c1.get(5), 0, 0, 0);
			long millis = c2.getTimeInMillis();
			millis -= millis % 1000L;
			c2.setTimeInMillis(millis);
			return c2.getTime();
		}
	}

	public static Date skipDateTime(Date d, int skipDay) {
		if (d == null) {
			return null;
		} else {
			Calendar calendar = Calendar.getInstance();
			calendar.setTime(d);
			calendar.add(5, skipDay);
			return calendar.getTime();
		}
	}

	public static String skipDateTime(String timeStr, int skipDay) {
		String pattern = "yyyy-MM-dd HH:mm:ss";
		Date d = parse(pattern, timeStr);
		Date date = skipDateTime(d, skipDay);
		return formatDateTime(pattern, date);
	}

	public static String skipDate(String dateStr, int skipDay) {
		String pattern = "yyyy-MM-dd";
		Date d = parse(pattern, dateStr);
		Date date = skipDateTime(d, skipDay);
		return formatDateTime(pattern, date);
	}

	public static String getTime(String timeStr, int skipDay, int skipHour, int skipMinute) {
		if (null == timeStr) {
			return null;
		} else {
			GregorianCalendar cal = new GregorianCalendar();
			cal.setTime(parse("yyyy-MM-dd HH:mm:ss", timeStr));
			cal.add(5, skipDay);
			cal.add(11, skipHour);
			cal.add(12, skipMinute);
			cal.get(8);
			SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			return dateFormat.format(cal.getTime());
		}
	}

	public static boolean dateCompare(String str) {
		boolean bea = false;
		SimpleDateFormat sdf_d = new SimpleDateFormat("yyyy-MM-dd");
		String isDate = sdf_d.format(new Date());

		try {
			Date date1 = sdf_d.parse(str);
			Date date0 = sdf_d.parse(isDate);
			if (date0.after(date1)) {
				bea = true;
			}
		} catch (ParseException var7) {
			bea = false;
		}

		return bea;
	}

	public static boolean monthCompare(String str) {
		boolean bea = false;
		SimpleDateFormat sdf_m = new SimpleDateFormat("yyyy-MM");
		String isMonth = sdf_m.format(new Date());

		try {
			Date date1 = sdf_m.parse(str);
			Date date0 = sdf_m.parse(isMonth);
			if (date0.after(date1)) {
				bea = true;
			}
		} catch (ParseException var7) {
			bea = false;
		}

		return bea;
	}

	public static boolean secondCompare(String str) {
		boolean bea = false;
		SimpleDateFormat sdf_d = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String isDate = sdf_d.format(new Date());

		try {
			Date date1 = sdf_d.parse(str);
			Date date0 = sdf_d.parse(isDate);
			if (date0.after(date1)) {
				bea = true;
			}
		} catch (ParseException var7) {
			bea = false;
		}

		return bea;
	}

	public static boolean secondCompare(String str1, String str2) {
		boolean bea = false;
		SimpleDateFormat sdf_d = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

		try {
			Date date1 = sdf_d.parse(str1);
			Date date0 = sdf_d.parse(str2);
			if (date0.after(date1)) {
				bea = true;
			}
		} catch (ParseException var7) {
			bea = false;
		}

		return bea;
	}

	public static String dateAdd(String type, int i) {
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String str = formatDateTime("yyyy-MM-dd HH:mm:ss");
		Calendar c = Calendar.getInstance();
		int d;
		if (type.equals("s")) {
			d = c.get(13);
			d += i;
			c.set(13, d);
			str = df.format(c.getTime());
		} else if (type.equals("d")) {
			d = c.get(5);
			d += i;
			c.set(5, d);
			str = df.format(c.getTime());
		}

		return str;
	}

	public static String getTime(int skipDay) {
		GregorianCalendar cal = new GregorianCalendar();
		cal.setTime(new Date());
		cal.add(5, skipDay);
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		return dateFormat.format(cal.getTime());
	}

	public static long subtraction(Date minuend, Date subtrahend) {
		long daterange = minuend.getTime() - subtrahend.getTime();
		long time = 86400000L;
		return daterange % time == 0L ? daterange / time : daterange / time + 1L;
	}

	public static long getM(Date date) {
		GregorianCalendar cal = new GregorianCalendar();
		cal.setTime(date);
		return (long)cal.get(7);
	}

	public static String getLastDate(String temp) {
		if (temp == null || temp.equals("")) {
			temp = "1";
		}

		int i = Integer.parseInt(temp);
		DateFormat dateFormat = DateFormat.getDateInstance(2);
		Calendar grc = Calendar.getInstance();
		grc.add(5, -i);
		return dateFormat.format(grc.getTime());
	}

	public static String getLastYearDate() {
		Calendar c = Calendar.getInstance();
		int y = c.get(1);
		String year = String.valueOf(y - 1);
		return year;
	}

	public static String getLastMonthDate() {
		Calendar c = Calendar.getInstance();
		int y = c.get(1);
		int m = c.get(2) + 1;
		String month = null;
		String year = String.valueOf(y);
		if (m > 1) {
			if (m > 10) {
				month = String.valueOf(m - 1);
			} else {
				month = "0" + String.valueOf(m - 1);
			}
		} else {
			year = String.valueOf(y - 1);
			month = String.valueOf(12);
		}

		return year + "-" + month;
	}



	public static boolean isLeapYear(int year) {
		return year % 4 == 0 && year % 100 != 0 || year % 4 == 0 && year % 400 == 0;
	}

	public static int getMonthsDays(int month, int year) {
		if (isLeapYear(year) && month == 2) {
			return 29;
		} else if (!isLeapYear(year) && month == 2) {
			return 28;
		} else {
			return month != 1 && month != 3 && month != 5 && month != 7 && month != 8 && month != 10 && month != 12 ? 30 : 31;
		}
	}

	public static String getWeekDay() {
		DateFormatSymbols symboles = new DateFormatSymbols(Locale.getDefault());
		symboles.setShortWeekdays(new String[]{"", "7", "1", "2", "3", "4", "5", "6"});
		SimpleDateFormat date = new SimpleDateFormat("E", symboles);
		return date.format(new Date());
	}

	public static int getYear() {
		Calendar c = Calendar.getInstance();
		return c.get(1);
	}

	public static int getMonth() {
		Calendar c = Calendar.getInstance();
		return c.get(2);
	}

	public static int getDay() {
		Calendar c = Calendar.getInstance();
		return c.get(5);
	}

	public static String getLastMonthDay(int lastmonths) {
		int month = getMonth() + 1;
		return month - lastmonths > 0 ? getYear() + "-" + (month - lastmonths) + "-1" : getYear() - 1 + "-" + (12 + month - lastmonths) + "-1";
	}


}
